﻿using System.Collections.Generic;
using System.Linq;
using DAL;
using Film = BLL.BO.Film;

namespace BLL
{
    public class ChallengerLogic : LogicBase
    {
        public ChallengerLogic(string connectionString): base(connectionString)
        {
        }

        public List<BLL.BO.Challenger> GetAllChallenger()
        {
            using (var context = new ZsKategDataContext(connectionString))
            {
                List<BO.Challenger> ret = new List<BO.Challenger>();

                foreach (var challenger in context.Challengers)
                {
                    var challengerBo = new BO.Challenger
                                           {
                                               Id = challenger.ID,
                                               Name = challenger.Name,
                                               Films = new List<Film>()
                                           };

                    foreach (var film in challenger.Megnezves.Select(m => m.Film))
                    {
                        challengerBo.Films.Add(new BO.Film
                        {
                            Id = film.ID,
                            Title = film.Title,
                            Length = film.Length,
                            IMDB = film.IMDB
                        });
                    }

                    ret.Add(challengerBo);
                }

                return ret;
            }
        }

        public BO.Challenger GetChallenger(int id)
        {
            using (var context = new ZsKategDataContext(connectionString))
            {
                var challenger = context.Challengers.Single(c => c.ID == id);

                if (challenger != null)
                {
                    var ret = new BO.Challenger
                                  {
                                      Id = challenger.ID,
                                      Name = challenger.Name,
                                      Films = new List<BO.Film>()
                    };

                    foreach (var film in challenger.Megnezves.Select(m => m.Film))
                    {
                        ret.Films.Add(new BO.Film
                                          {
                                              Id = film.ID,
                                              Title = film.Title,
                                              Length = film.Length,
                                              IMDB = film.IMDB
                                          });
                    }

                    return ret;
                }

                return null;
            }
        }
    }
}
